package apis

import (
	log "github.com/sirupsen/logrus"
	"k8s.io/client-go/kubernetes"
	tappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
	corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"os"
	"path/filepath"
)

var client *kubernetes.Clientset

func Client() *kubernetes.Clientset {
	return client
}

func AppsV1() tappsv1.AppsV1Interface {
	return client.AppsV1()
}

func CoreV1() corev1.CoreV1Interface {
	return client.CoreV1()
}

func Config() *rest.Config {
	path, _ := os.Getwd()
	kubeconfig := filepath.Join(
		path, "apis/.kube", "config",
	)
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		log.Error(err)
		panic(err)
	}
	return config
}

func Init() {
	var err error
	client, err = kubernetes.NewForConfig(Config())
	if err != nil {
		panic(err)
	}
}
